<template>
  <div class="container">
    <div class="container-content">
      <vab-query-form>
        <vab-query-form-top-panel>
          <el-form
            :inline="true"
            label-width="80px"
            :model="tableOptions.queryForm"
          >
            <el-form-item label="岗位编码">
              <el-input
                v-model="tableOptions.queryForm.jobCode"
                placeholder="岗位编码"
              />
            </el-form-item>
            <el-form-item label="岗位名称">
              <el-input
                v-model="tableOptions.queryForm.jobName"
                placeholder="岗位名称"
              />
            </el-form-item>
            <el-form-item>
              <el-button
                icon="refresh-left"
                type="warning"
                @click="handleResetQuery"
              >
                重置
              </el-button>
              <el-button icon="Search" type="primary" @click="queryTableData">
                查询
              </el-button>
            </el-form-item>
          </el-form>
        </vab-query-form-top-panel>
        <vab-query-form-left-panel :span="24">
          <el-button
            v-auth="['job/xz']"
            icon="circle-plus"
            type="primary"
            @click="handleAdd"
          >
            添加
          </el-button>
          <el-button
            v-auth="['job/sc']"
            icon="remove"
            type="danger"
            @click="handleDelete"
          >
            批量删除
          </el-button>
        </vab-query-form-left-panel>
      </vab-query-form>
      <el-table
        ref="tableRef"
        v-loading="tableOptions.loading"
        border
        stripe
        :data="tableOptions.dataList"
        :height="tableOptions.height"
        :cell-style="tableStyle.cellStyle"
        :header-cell-style="tableStyle.headerCellStyle"
        @sort-change="handleSortChange"
        @selection-change="handleSelectionChange"
        @row-click="handleRowClick"
      >
        <el-table-column
          align="center"
          show-overflow-tooltip
          type="selection"
          width="50"
        />
        <el-table-column label="序号" type="index" width="55">
          <template #default="{ $index }">
            {{ $index + 1 }}
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          label="岗位编码"
          prop="jobCode"
          sortable
        />
        <el-table-column
          align="center"
          label="岗位名称"
          prop="jobName"
          sortable
        />
        <el-table-column
          align="center"
          label="排序号"
          prop="orderId"
          sortable
        />
        <el-table-column align="center" label="岗位类别" prop="classIdDesc" />
        <el-table-column align="center" label="创建人" prop="createUserName" />
        <el-table-column
          align="center"
          :formatter="dateFormat"
          label="创建时间"
          prop="createTime"
        />
        <el-table-column align="center" fixed="right" label="操作" width="240">
          <template #default="{ row }">
            <el-button
              icon="Document"
              size="small"
              type="primary"
              @click="handleDetail(row)"
            >
              详情
            </el-button>
            <el-button
              icon="Edit"
              v-auth="['job/xg']"
              size="small"
              type="warning"
              @click="handleEdit(row)"
            >
              编辑
            </el-button>
            <el-button
              icon="Remove"
              v-auth="['job/sc']"
              size="small"
              type="danger"
              @click="handleDelete(row)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        :current-page="tableOptions.pagination.current"
        :layout="tableOptions.pagination.layout"
        :page-size="tableOptions.pagination.size"
        :total="tableOptions.pagination.total"
        @current-change="handleCurrentChange"
        @size-change="handleSizeChange"
      />
      <job-edit ref="editRef" width="40%" @confirm="queryTableData" />
    </div>
  </div>
</template>
<script setup name="Job">
import { useMessage, useMessageBox } from "@/hooks/message";
import { dateFormat } from "@/utils/common";
import { queryJobTable, deleteJob } from "@/api/admin/org/job/";
import jobEdit from "./components/jobEdit.vue";
import { useTable } from "@/hooks/table";

const tableRef = ref(null);
const editRef = ref(null);

const tableOptions = reactive({
  queryForm: {
    jobCode: "",
    jobName: ""
  },
  queryFunction: queryJobTable,
  descs: ["create_time"]
});

const {
  tableStyle,
  queryTableData,
  handleSelectionChange,
  handleResetQuery,
  handleRowClick,
  handleSizeChange,
  handleCurrentChange,
  handleSortChange
} = useTable(tableOptions, tableRef);

const handleAdd = () => {
  editRef.value.show();
};

const handleDelete = row => {
  if (row && row.id) {
    useMessageBox()
      .confirm("删除的岗位将无法恢复，是否继续")
      .then(async () => {
        const { msg } = await deleteJob(row.id);
        useMessage().success(msg);
        await queryTableData();
      });
  } else {
    if (tableOptions.dataSelections.length > 0) {
      const ids = state.selectRows.map(item => item.id).join();
      useMessageBox().confirm(
        "删除的岗位将无法恢复，是否继续",
        null,
        async () => {
          const { msg } = await deleteJob(ids);
          useMessage().success(msg);
          await queryTableData();
        }
      );
    } else {
      useMessage().warning("未选中任何行");
    }
  }
};

const handleEdit = row => {
  if (row.id) {
    editRef.value.show(row, false);
  } else {
    useMessage().warning("未选中任何行");
  }
};

const handleDetail = row => {
  if (row.id) {
    editRef.value.show(row, true);
  } else {
    useMessage().warning("未选中任何行");
  }
};
</script>
<style lang="scss" scoped></style>
